<template>
  <el-card>
    <div slot="header" class="clearfix">
      <el-form :inline="true">
        <!-- <el-form-item>
          <el-input placeholder="请输入内容" prefix-icon="el-icon-search" v-model="query.productTypeName">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" plain @click="loadMatInfos"></el-button>
        </el-form-item> -->
        <el-form-item>
          <el-button type="success" @click="dialog.addShow = true"
            >新建物料定义</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :data="matInfoList"
      border
      style="width: 100%"
      :cell-style="{ 'text-align': 'center' }"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column prop="matName" label="物料名称"> </el-table-column>
      <el-table-column prop="matTypeName" label="物料类型"> </el-table-column>
      <el-table-column prop="matNum" label="物料数量"> </el-table-column>
      <el-table-column prop="matUnit" label="物料单位"> </el-table-column>
      <el-table-column prop="warehouseName" label="仓库名称"> </el-table-column>
      <el-table-column prop="warehouseAddr" label="仓库地址"> </el-table-column>
      <el-table-column label="操作" width="300px">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="small"
            @click="handleEdit(scope.row.matId)"
            >编辑</el-button
          >
          <el-popconfirm
            title="这是一段内容确定删除吗？"
            @confirm="handleDelete(scope.row.matId)"
          >
            <el-button
              type="danger"
              slot="reference"
              size="small"
              icon="el-icon-delete"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
      <el-table-column label="入库">
        <template slot-scope="scope">
          <el-button
            type="success"
            size="small"
            icon="el-icon-delete"
            @click="handleInput(scope.row.matId)"
            >入库</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="出库">
        <template slot-scope="scope">
          <el-button
            type="warning"
            size="small"
            icon="el-icon-delete"
            @click="handleOutput(scope.row.matId)"
            >出库</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- --------------------------添加---------------------- -->
    <el-dialog title="添加物料定义" :visible.sync="dialog.addShow" width="30%">
      <el-form ref="addForm" :model="addForm" label-width="80px">
        <el-form-item label="物料名称">
          <el-input
            v-model="addForm.matName"
            placeholder="请输入物料名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="物料类型">
          <el-select
            v-model="addForm.matTypeId"
            placeholder="请选择类型"
            style="width: 100%"
          >
            <el-option
              v-for="item in matTypes"
              :key="item.matTypeName"
              :label="item.matTypeName"
              :value="item.matTypeId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物料单位">
          <el-input
            v-model="addForm.matUnit"
            placeholder="请输入物料单位"
          ></el-input>
        </el-form-item>
        <el-form-item label="选择仓库">
          <el-select
            v-model="addForm.warehouseId"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in warehouses"
              :key="item.warehouseName"
              :label="item.warehouseName"
              :value="item.warehouseId"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog.addShow = false">取 消</el-button>
        <el-button type="primary" @click="add()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- --------------------------修改---------------------- -->
    <el-dialog
      title="添加物料定义"
      :visible.sync="dialog.updateShow"
      width="30%"
    >
      <el-form ref="updateForm" :model="updateForm" label-width="80px">
        <el-form-item label="物料名称">
          <el-input
            v-model="updateForm.matName"
            placeholder="请输入物料名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="物料类型">
          <el-select
            v-model="updateForm.matTypeId"
            placeholder="请选择类型"
            style="width: 100%"
          >
            <el-option
              v-for="item in matTypes"
              :key="item.matTypeName"
              :label="item.matTypeName"
              :value="item.matTypeId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物料单位">
          <el-input
            v-model="updateForm.matUnit"
            placeholder="请输入物料单位"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog.updateShow = false">取 消</el-button>
        <el-button type="primary" @click="updateMat()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- -----------------入库---------------------- -->
    <el-dialog
      title="物料入库"
      :visible.sync="dialog.updateInputShow"
      width="30%"
    >
      <el-form
        ref="updateStorageForm"
        :model="updateStorageForm"
        label-width="120px"
      >
        <el-form-item label="物料入库数量">
          <el-input-number
            :min="1"
            size="medium"
            v-model="updateStorageForm.matNum"
            style="width: 100%"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="入库人">
          <el-cascader
            v-model="optionList"
            :options="options"
            @change="handleOptionChange"
            style="width: 100%"
          >
          </el-cascader>
        </el-form-item>
        <el-form-item
          label="物料仓储id"
          hidden
          v-model="updateStorageForm.matId"
        >
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog.updateInputShow = false">取 消</el-button>
        <el-button type="primary" @click="updateInput()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- -----------------出库---------------------- -->
    <el-dialog
      title="物料出库"
      :visible.sync="dialog.updateOutputShow"
      width="30%"
    >
      <el-form
        ref="updateStorageForm"
        :model="updateStorageForm"
        label-width="120px"
      >
        <el-form-item label="物料出库数量">
          <el-input-number
            :min="1"
            size="medium"
            v-model="updateStorageForm.matNum"
            style="width: 100%"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="出库人">
          <el-cascader
            v-model="optionList"
            :options="options"
            @change="handleOptionChange"
            style="width: 100%"
          >
          </el-cascader>
        </el-form-item>
        <el-form-item label="物料仓储id" hidden> </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog.updateOutputShow = false">取 消</el-button>
        <el-button type="primary" @click="updateOutput()">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      options: [],
      optionList: [],
      updateStorageForm: {},
      updateForm: {},
      addForm: {},
      dialog: {
        addShow: false,
        updateShow: false,
        updateInputShow: false,
        updateOutputShow: false,
      },
      matInfoList: [],
      matTypes: [],
      warehouses: [],
    };
  },
  methods: {
    handleOptionChange() {
      this.inspInfo.editInfo.workNo =
        this.optionList[this.optionList.length - 1];
    },
    loadMatInfos() {
      this.$axios.get("/ms").then((res) => {
        this.matInfoList = res.data.data.list;
      });
    },

    add() {
      this.$axios.post("/mt", this.addForm).then((res) => {
        if (res.data.code == 200) {
          this.addForm = {};
          this.$message.success(res.data.message);
          this.dialog.addShow = false;
          this.loadMatInfos();
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
    updateMat() {
      this.$axios.put("/ms", this.updateForm).then((res) => {
        if (res.data.code == 200) {
          this.loadMatInfos();
        } else {
          this.$message.error(res.data.message);
        }
      });
      this.dialog.updateShow = false;
      this.updateForm = {};
    },
    handleInput(matId) {
      this.handleMatEdit(matId);
      this.dialog.updateInputShow = true;
    },
    handleOutput(matId) {
      this.handleMatEdit(matId);
      this.dialog.updateOutputShow = true;
    },
    handleEdit(matId) {
      this.$axios.get("/ms/" + matId).then((res) => {
        this.updateForm = res.data.data.mat;
      });
      this.dialog.updateShow = true;
    },
    handleMatEdit(matId) {
      this.loadOptions();
      this.$axios.get("/ms/" + matId).then((res) => {
        this.updateStorageForm = res.data.data.mat;
        this.updateStorageForm.matNum = 1;
      });
    },
    handleDelete(matId) {
      this.$axios.delete("proinfo/" + matId).then((res) => {
        if (res.data.code == 200) {
          this.$message.success(res.data.message);
          this.loadMatInfos();
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
    loadTypes() {
      this.$axios.get("/ms").then((res) => {
        this.matTypes = res.data.data.list;
      });
    },
    loadWarehouses() {
      this.$axios.post("/storages/queryAll", {}).then((res) => {
        if (res.data.code == 200) {
          this.warehouses = res.data.data.list; //当前页的数据
          this.warehouses.forEach((element) => {
            element.status = element.status == 0 ? true : false;
          });
        }
      });
    },
    loadOptions() {
      this.$axios.post("/emps/4").then((res) => {
        if (res.data.code == 200) {
          this.options = res.data.data.vos;
        }
      });
    },
    updateInput() {
      this.dialog.updateInputShow = false;
      this.updateStorage(1);
    },
    updateOutput() {
      this.dialog.updateOutputShow = false;
      this.updateStorage(0);
    },
    updateStorage(mode) {
      this.updateStorageForm.inStatus = mode;
      this.$axios
        .put("/ms/updateStorage", this.updateStorageForm)
        .then((res) => {
          if (res.data.code == 200) {
          }
        });
    },
  },
  mounted() {
    this.loadMatInfos();
    this.loadTypes();
    this.loadWarehouses();
  },
};
</script>